import {defineStore} from 'pinia';

const statusOption = [
    {label: "正常", value: "0"},
    {label: "停用", value: "1"}
]

const API = {
    LIST: '/role/page',
    ENABLED: "/role/enabled",
    DISABLED: "/role/disabled",
    ADD: "/role",
    INFO: "/role/details",
    MODIFY: "/role",
    REMOVE: "/role",
    MENULIST: "/menu/tree"
}

const COLUMNS = [
    {field: "roleId", title: "角色编号", align: "center"},
    {field: "roleName", title: "角色名称", align: "center"},
    {field: "roleCode", title: "权限字符", align: "center"},
    {field: "roleDesc", title: "角色说明", align: "left"},
    {field: "createTime", title: "创建时间", align: "center"},
]

const SEARCH_FORM = [
    {field: "roleName", title: "角色名称", placeholder: "请输入角色名称"},
    {field: "roleCode", title: "权限字符", placeholder: "请输入权限字符"},
    // {field: "status", title: "状态", options: statusOption},
    // {mapper: [{field: "beginTime", placeholder: "请选择开始日期"}, {field: "endTime", placeholder: "请选择结束日期"}], title: "创建时间", type: 'dateRange'},
]

const OPTIONS = [
    {emit: "modify", title: "修改"},
    {emit: "auth", title: "授权"},
    {emit: "remove", title: "删除"},
    // {
    //     children: [
    //         {emit: "assignUser", title: "分配用户"},
    //     ]
    // },
]

const store = {
    id: "sysRole",
    state(){
        return {
            table: null,
            pageNo: 1,
            pageSize: 10,
            searchForm: {}
        }
    },

    getters: {

    },

    actions: {
        init(table) {
            this.table = table;
            this.table.setColumns(COLUMNS);
            this.table.setSearchForm(SEARCH_FORM);
            this.table.setOptions(OPTIONS);
        },

        refresh(isFirstPage) {
            if (isFirstPage) {
                this.getList(1, 10)
                return
            }
            this.getList(this.pageNo, this.pageSize)
        },

        /**
         * 列表数据
         * @param pageNo
         * @param pageSize
         * @param condition
         * @returns {Promise<void>}
         */
        async getList(pageNo, pageSize, condition) {
            let params = {};
            params.pageNo = pageNo ? pageNo : 1;
            params.pageSize = pageSize ? pageSize : 10;
            for (let key in condition) {
                if(! Helper.empty(condition[key])) {
                    params[key] = condition[key];
                }
            }

            Msg.loading("加载中...")
            let {data} = await Http.get(API.LIST, params);
            Msg.loading(false)

            this.pageNo = data.current
            this.pageSize = data.size
            let dataList = []
            if(! Helper.emptyArray(data.records)) {
                dataList = data.records
            }
            this.table.setData(dataList);
            this.table.setPagination(data.current, data.size, data.total);
        },

        /**
         * 状态启用
         * @param params
         * @returns {Promise<boolean>}
         */
        async enabled(params) {
            Msg.loading("加载中...")
            let {code} = await Http.post(API.ENABLED, params);
            Msg.loading(false);

            if(code == 200) {
                Msg.success("保存成功");
                this.refresh();
                return true
            } else {
                Msg.error("保存失败，请重新尝试")
                return false
            }
        },

        /**
         * 状态禁用
         * @param params
         * @returns {Promise<boolean>}
         */
        async disabled(params) {
            Msg.loading("加载中...")
            let {code} = await Http.post(API.DISABLED, params);
            Msg.loading(false);

            if(code == 200) {
                Msg.success("保存成功");
                this.refresh();
                return true
            } else {
                Msg.error("保存失败，请重新尝试")
                return false
            }
        },

        /**
         * 新增
         * @param params
         * @returns {Promise<undefined>}
         */
        async add(params) {
            Msg.loading("正在提交...")
            let {data} = await Http.post(API.ADD, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.refresh(true);
            } else {
                Msg.error("保存失败, 请重新尝试")
            }
            return data
        },

        /**
         * 修改
         * @param params
         * @returns {Promise<undefined>}
         */
        async modify(params) {
            Msg.loading("正在提交...")
            let {data} = await Http.put(API.MODIFY, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.refresh(true);
            } else {
                Msg.error("保存失败, 请重新尝试")
            }
            return data
        },

        /**
         * 获取详情
         * @param id
         * @returns {Promise<undefined|boolean>}
         */
        async getInfo(id) {
            Msg.loading("加载中...");
            let {data} = await Http.get(API.INFO + "/" + id)
            Msg.loading(false);

            if (! Helper.emptyJson(data)) {
                return data
            }
            return false
        },

        /**
         * 删除
         * @param id
         * @returns {Promise<undefined>}
         */
        async remove(id) {
            Msg.loading("正在删除...");
            let {data} = await Http.delete(API.REMOVE, [id])
            Msg.loading(false)

            if(data) {
                Msg.success("删除成功");
                this.refresh()
            } else {
                Msg.error("删除失败，请重新尝试");
            }
            return data
        },

        /**
         * 菜单
         * @returns {Promise<undefined|*[]>}
         */
        async getMenuList() {
            Msg.loading("加载中...");
            let {data} = await Http.get(API.MENULIST)
            Msg.loading(false);

            if (! Helper.emptyArray(data)) {
                return data
            }
            return []
        },

        /**
         * 授权
         * @param id
         * @param menuIds
         * @returns {Promise<void>}
         */
        async setAuth(id, menuIds) {
            Msg.loading("加载中...")
            let {code, data} = await Http.put("/role/menu", {roleId: id, menuIds});
            Msg.loading(false)

            if(code == 200) {
                if(! data) {
                    Msg.error("授权失败");
                }else {
                    Msg.success("授权成功");
                }
            }
        }
    },
}

export default defineStore(store)
